<template>
  <!-- 数字加减的组件 -->
  <div class="count-box">
    <button @click="handle(value - 1)" class="minus" :disabled="value === 1">-</button>
    <input type="text" class="inp" :value="value" @change="handleChange">
    <button @click="handle(value + 1)" class="add">+</button>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    handle (v) {
      this.$emit('input', v)
    },
    handleChange (e) {
      const put = +e.target.value

      // 判断是否为数字或小数
      if (isNaN(put) || put < 1 || put % 1 !== 0) {
        e.target.value = this.value
        return
      }
      this.$emit('input', put)
    }
  }
}
</script>

<style lang="less" scoped>
.count-box {
  width: 110px;
  display: flex;

  .add,
  .minus {
    width: 30px;
    height: 30px;
    background-color: #f1f1f1;
    outline: none;
    border: none;
  }

  .inp {
    width: 40px;
    height: 30px;
    text-align: center;
    background-color: #f1f1f1;
    margin: 0 5px;
    outline: none;
    border: none;
  }
}
</style>
